<template>
    <el-drawer :append-to-body="true" title="审批人设置" v-model="visible" class="set_promoter"  :with-header="false" :size="680">
        <span class="drawer-title">审批人设置</span> 
        <div class="demo-drawer__content">
            <div class="drawer_content">
                <div class="approver_content">
                    <p><p style="font-weight: 100;font-size: small;color: red;">*测试环境，仅开放指定人员</p></p>
                    <el-radio-group v-model="approverConfig.setType" class="clear" @change="changeType">
                        <el-radio v-for="({ value, label }) in setTypes" :value="value">{{ label }}</el-radio> 
                    </el-radio-group>
               
                    <el-button type="primary" @click="addApprover" v-if="approverConfig.setType == 5">添加/修改人员</el-button>
                    <p class="selected_list" v-if="approverConfig.setType == 5">
                        <span v-for="(item, index) in approverConfig.nodeApproveList" :key="index">{{ item.name }}
                            <img src="@/assets/images/add-close1.png"
                                @click="$func.removeEle(approverConfig.nodeApproveList, item, 'targetId')">
                        </span>
                        <a v-if="approverConfig.nodeApproveList.length != 0"  @click="approverConfig.nodeApproveList = []">清除</a>
                    </p>
                </div>
                <div class="approver_manager" v-if="approverConfig.setType == 3">
                    <p>
                        <span>发起人的：</span>
                        <select v-model="approverConfig.directorLevel">
                            <option v-for="item in directorMaxLevel" :value="item" :key="item">
                                {{ item == 1 ? '直接' : '第' + item + '级' }}主管</option>
                        </select>
                    </p>
                    <p class="tip">找不到主管时，由上级主管代审批</p>
                </div>

                <div class="approver_self_select" v-show="approverConfig.setType == 4">
                    <el-button type="primary" @click="addRoleApprover">添加/修改角色</el-button>
                    <p class="selected_list">
                        <span v-for="(item, index) in approverConfig.nodeApproveList" :key="index">{{ item.name }}
                            <img src="@/assets/images/add-close1.png"
                                @click="$func.removeEle(approverConfig.nodeApproveList, item, 'targetId')">
                        </span>
                        <a v-if="approverConfig?.nodeApproveList?.length != 0"
                            @click="approverConfig.nodeApproveList = []">清除</a>
                    </p>
                </div>
                <div class="approver_self_select" v-show="approverConfig.setType == 14">
                    <el-button type="primary" @click="addRoleApprover">添加/修改部门</el-button>
                    <p class="selected_list">
                        <span v-for="(item, index) in approverConfig.nodeApproveList" :key="index">{{ item.name }}
                            <img src="@/assets/images/add-close1.png"
                                @click="$func.removeEle(approverConfig.nodeApproveList, item, 'targetId')">
                        </span>
                        <a v-if="approverConfig.nodeApproveList?.length != 0"
                            @click="approverConfig.nodeApproveList = []">清除</a>
                    </p>
                </div>
               
                <div class="approver_self" v-if="approverConfig.setType == 6">
                    <p>HRBP选择设置</p>
                    <div class="approver_some">
                        <el-select  v-model="checkedHRBP" placeholder="请选择">
                            <el-option
                                v-for="item in hrbpOptions" required
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </div> 
                </div>
                <div class="approver_self" v-if="approverConfig.setType == 12">
                    <p>该审批节点设置“发起人自己”后，审批人默认为发起人</p>
                </div>
                <div class="approver_self" v-if="approverConfig.setType == 13">
                    <p>该审批节点设置“直属领导”后，审批人默认为发起人的直属领导</p>
                </div>

                <el-tabs v-model="activeName" class="set-tabs">
                    <el-tab-pane label="基础设置" name="baseTab">                        
                        <div class="approver_some">
                            <p>多人审批时采用的审批方式</p>
                            <el-radio-group v-model="approverConfig.signType" class="clear">
                                <el-radio :value="1">会签（需所有审批人同意，不限顺序）</el-radio>
                                <br />
                                <el-radio :value="2">或签（只需一名审批人同意或拒绝即可）</el-radio>
                                <br />
                                <el-radio :value="3" v-if="approverConfig.setType == 5">顺序会签（需要所有审批人同意，根据前端传入的顺序）</el-radio>
                            </el-radio-group>
                        </div>
                        <div class="approver_some">
                            <p>审批人为空时</p>
                            <el-radio-group v-model="approverConfig.noHeaderAction" class="clear">
                                <el-radio :value="1">自动审批通过/不允许发起</el-radio>
                                <br />
                                <el-radio :value="2">转交给审核管理员</el-radio>
                            </el-radio-group>
                        </div>
                    </el-tab-pane>
                    <el-tab-pane label="按钮设置" name="btnTab">                    
                        <div class="approver_some">
                            <p>【审批页面】按钮权限显示控制</p> 
                            <el-checkbox-group class="clear"  v-model="checkApprovalPageBtns"> 
                                <el-checkbox style="margin: 0 15px 0 0;"  border
                                 v-for="opt in approvalPageButtons"  
                                :value="opt.value" 
                                :disabled="opt.type === 'default'"
                                 @change="handleCheckedButtonsChange(opt.value)"
                                >{{opt.label}}</el-checkbox>
                            </el-checkbox-group> 

                            <!-- <el-checkbox-group v-if="afterSignUpWayVisible" class="approver_some" v-model="checkAfterSignUpWay"> 
                                <el-checkbox style="margin: 0 15px 0 0;"  border  :value="1"  @change="handleAfterSignUpWay(opt.value)" >是否回到加批人</el-checkbox>
                            </el-checkbox-group>  -->

                            <el-checkbox border style="margin-top: 10px;" 
                            v-if="afterSignUpWayVisible"  
                            v-model="checkAfterSignUpWay"
                            @change="handleAfterSignUpWay(checkAfterSignUpWay)">是否回到加批人</el-checkbox>
                        </div> 
                    </el-tab-pane> 
                </el-tabs> 
            </div>
            <div class="demo-drawer__footer clear">
                <el-button type="primary" @click="saveApprover">确 定</el-button>
                <el-button @click="closeDrawer">取 消</el-button>
            </div>
            <employees-dialog v-model:visible="approverVisible" :data="checkedList" @change="sureApprover" />
            <role-dialog v-model:visible="approverRoleVisible" :data="checkedRoleList" @change="sureRoleApprover" />
        </div>
    </el-drawer>
</template>
<script setup>
import { ref, watch, computed } from 'vue' 
import $func from '@/utils/flow/index'
import { setTypes,hrbpOptions,approvalPageButtons } from '@/utils/flow/const'
import { useStore } from '@/store/modules/workflow'
import employeesDialog from '../dialog/employeesDialog.vue'
import roleDialog from '../dialog/roleDialog.vue' 
let store = useStore()
let props = defineProps({
    directorMaxLevel: {
        type: Number,
        default: 0
    }
});

const activeName = ref('baseTab')
let approverConfig = ref({})
let approverVisible = ref(false)
let approverRoleVisible = ref(false)
let checkedRoleList = ref([])
let checkedList = ref([]) 
let checkApprovalPageBtns = ref([])
let checkedHRBP = ref('') 
let approvalPageBtns = ref([]) 

let afterSignUpWayVisible = computed(()=> approverConfig.value?.isSignUp == 1) 
let checkAfterSignUpWay = ref(false)
 
let approverConfig1 = computed(() => store.approverConfig1)
let approverDrawer = computed(() => store.approverDrawer)
let visible = computed({
    get() {
        return approverDrawer.value
    },
    set() {
        closeDrawer()
    }
}) 
 
watch(approverConfig1, (val) => { 
    approverConfig.value = val.value;   
    checkApprovalPageBtns.value = val.value.buttons?.approvalPage;   
})
watch(approverConfig1, (val) => { 
    approvalPageBtns.value = val.value.buttons?.approvalPage;   
    if (val.value.nodeProperty == 6) {
        val.value.nodeApproveList.length > 0 && val.value.nodeApproveList.map(item => {
            checkedHRBP.value = item.targetId
        })
    }   
})
watch(() => approverConfig.value?.property?.afterSignUpWay, (newVal, oldVal) => { 
        checkAfterSignUpWay.value = newVal == 1?true:false
    } 
)
watch(checkedHRBP, (val) => {  
    let labelName = hrbpOptions.find(item => item.value == val)?.label; 
    approverConfig.value.nodeApproveList = [{"type":6,"targetId":val,"name":labelName}]; 
  
})

const changeType = (val) => {
    //console.log('typevale=====>',val);
    
    approverConfig.value.nodeApproveList = [];
    approverConfig.value.signType = 1;
    approverConfig.value.noHeaderAction = 2;
    checkedHRBP.value = ''; 
    if (val == 3) {
        approverConfig.value.directorLevel = 1;
    } else {

    }
}
const addApprover = () => {
    approverVisible.value = true;
    checkedList.value = approverConfig.value.nodeApproveList
}
const addRoleApprover = () => {
    approverRoleVisible.value = true;
    checkedRoleList.value = approverConfig.value.nodeApproveList
}
const sureApprover = (data) => {
    approverConfig.value.nodeApproveList = data;
    approverVisible.value = false;
}
const sureRoleApprover = (data) => {
    approverConfig.value.nodeApproveList = data;
    approverRoleVisible.value = false;
}
const saveApprover = () => { 
    approverConfig.value.error = !$func.setApproverStr(approverConfig.value) 
    //console.log('approverConfig.===saveApprover=======',JSON.stringify(approverConfig.value)) 
    store.setApproverConfig({
        value: approverConfig.value,
        flag: true,
        id: approverConfig1.value.id
    })
    closeDrawer()
}
const closeDrawer = () => {
    store.setApprover(false)
}
  
const handleCheckedButtonsChange = (val) =>  {   
    const index = approvalPageBtns.value.indexOf(val); 
    index < 0 ? approvalPageBtns.value.push(val) : approvalPageBtns.value.splice(index, 1);    
    const isAddStep = approvalPageBtns.value.indexOf(19); 
    if(isAddStep >= 0){
        approverConfig.value.isSignUp = 1;
    }else{
        approverConfig.value.isSignUp = 0;
    }
}
const handleAfterSignUpWay = (val) => {
    const isTure = approverConfig.value.hasOwnProperty("property");
    if (isTure) {
        if (approverConfig.value.property.hasOwnProperty("afterSignUpWay")) {
            approverConfig.value.property.afterSignUpWay = val ? 1 : 2;
        } else {
            Object.assign(approverConfig.value.property, { afterSignUpWay: val ? 1 : 2 });
        }
    } else {
        Object.assign(approverConfig.value, {
            property: { afterSignUpWay: val ? 1 : 2 }
        });
    }
    console.log('approverConfig.value=============',JSON.stringify(approverConfig.value))
}

</script>
<style scoped lang="scss">
@import "@/assets/styles/flow/dialog.scss";

.el-drawer__header{
    margin-bottom: 5px !important;
}

.selected_list {
    margin-bottom: 20px;
    line-height: 30px;
}

.selected_list span {
    margin-right: 10px;
    padding: 3px 6px 3px 9px;
    line-height: 12px;
    white-space: nowrap;
    border-radius: 2px;
    border: 1px solid rgba(220, 220, 220, 1);
}

.selected_list img {
    margin-left: 5px;
    width: 7px;
    height: 7px;
    cursor: pointer;
}

.selected_list a {
    font-size: 10 !important;
    color: #46a6fe;
    cursor: pointer;
}
.el-tabs { 
    margin-left: 20px !important;
}
.set_promoter {
    .approver_content {
        padding-bottom: 10px;
        border-bottom: 1px solid #f2f2f2;
    }

    .approver_self_select,
    .approver_content {
        .el-button {
            margin-bottom: 20px;
        }
    }
    .approver_content,
    .approver_some,
    .approver_self_select {
        padding-top:10px;
        .el-radio-group {
            display: unset;
        }

        .el-radio {
            width: 27%;
            margin-bottom: 20px;
            height: 16px;
        }
    }
    .approver_manager p {
        line-height: 32px;
    }
    .approver_manager select {
        width: 420px;
        height: 32px;
        background: rgba(255, 255, 255, 1);
        border-radius: 4px;
        border: 1px solid rgba(217, 217, 217, 1);
    }
    .approver_manager p.tip {
        margin: 10px 0 22px 0;
        font-size: 12px;
        line-height: 16px;
        color: #f8642d;
    }
    .approver_self {
        padding: 28px 20px;
    }
    .approver_self_select,
    .approver_manager,
    .approver_content {
        padding: 20px 20px 0;
    }

    .approver_manager p:first-of-type,
    .approver_some p {
        line-height: 19px;
        font-size: 14px;
        margin-bottom: 14px;
    }
    .approver_self_select h3 {
        margin: 5px 0 20px;
        font-size: 14px;
        font-weight: bold;
        line-height: 19px;
    }
}
</style>